/**
 * 
 */
package com.ai.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.common.controller.BaseController;
import com.ai.common.model.Pagination;
import com.ai.common.model.Result;
import com.ai.mmd.system.dto.GkcityRelationDTO;
import com.ai.service.system.model.GkcityRelationModel;
import com.ai.service.system.service.IGkcityRelationService;

/**
 * 港口
 * 
 * @author Administrator
 *
 */
@RequestMapping("re")
@Controller("com.ai.controller.system.RelationController")
public class RelationController extends BaseController {

	/**
	 * 列表
	 * @param gkcityRelationDTO
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("list")
	public Pagination<GkcityRelationModel> list(@ModelAttribute GkcityRelationDTO gkcityRelationDTO)
			throws Exception {
		if (gkcityRelationDTO.getSort()!=null) {
			gkcityRelationDTO.setSort(gkcityRelationDTO.getSort().concat(" " + gkcityRelationDTO.getOrder()));
		}else {
			gkcityRelationDTO.setSort("id desc");
		}
		return igkcityRelationService.getPagination(gkcityRelationDTO);
	}
	
	/**
	 * 关联
	* @date 2016年6月27日 下午9:31:40
	* @param CclxInfoDTO
	* @return
	* @throws Exception
	* List<positionModel>
	 */
	@ResponseBody
	@RequestMapping("neworedit")
	public Result neworedit(@RequestBody List<GkcityRelationDTO> list)
			throws Exception {
		Result rs = new Result(true);
		
		//获得数据库该城市绑定的港口
		List<Integer> dbgklist = new ArrayList<Integer>();
		Integer cityid = list.get(0).getCityid();
		GkcityRelationDTO gkcityRelationDTO = new GkcityRelationDTO();
		gkcityRelationDTO.setCityid(cityid);
		gkcityRelationDTO.setSfsc(0);
		List<GkcityRelationModel> reList = igkcityRelationService.getList(gkcityRelationDTO);
		if (reList.size()>0) {
			for (GkcityRelationModel gkcityRelationModel : reList) {
				dbgklist.add(gkcityRelationModel.getGkid());
			}
		}
		
		//获得前台港口列表
		List<Integer> frontgklist = new ArrayList<Integer>();
		for (GkcityRelationDTO gDto : list) {
			frontgklist.add(gDto.getGkid());
		}
		
		//备份前台
		List<Integer> backup = new ArrayList<Integer>();
		backup.addAll(frontgklist);
		
		//前台与数据库的差集，新增
		frontgklist.removeAll(dbgklist);
		GkcityRelationModel gkcityRelationModel = null;
		for (Integer gkid : frontgklist) {
			gkcityRelationModel = new GkcityRelationModel();
			gkcityRelationModel.setCityid(cityid);
			gkcityRelationModel.setGkid(gkid);
			gkcityRelationModel.setCjsj(new Date());
			igkcityRelationService.insert(gkcityRelationModel);
		}
		
		//数据库与前台的差集，删除
		dbgklist.removeAll(backup);
		for (Integer gkid : dbgklist) {
			GkcityRelationDTO gRelationDTO = new GkcityRelationDTO();
			gRelationDTO.setCityid(cityid);
			gRelationDTO.setGkid(gkid);
			gRelationDTO.setSfsc(0);
			gkcityRelationModel = igkcityRelationService.getOne(gRelationDTO);
			gkcityRelationModel.setSfsc(1);
			gkcityRelationModel.setGxsj(new Date());
			igkcityRelationService.updateByPrimaryKey(gkcityRelationModel);
		}
				
		return rs;
	}
	
	/**
     * 跳转
     * @return
     * @throws Exception
     */
    @RequestMapping("forward")
    public String forward() throws Exception{
        return "system/re/list";
    }

	@Resource(name = "igkcityrelationService")
	private IGkcityRelationService igkcityRelationService;

}
